package me.aartikov.alligatorcompiler;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Types;

public class ProcessingUtils {
    private Types typeUtils;

    public ProcessingUtils(ProcessingEnvironment processingEnv) {
        typeUtils = processingEnv.getTypeUtils();
    }

    public boolean isAssignableFrom(TypeElement classElement, String className) {
        TypeElement currentClassElement = classElement;
        System.out.println("开始        "+className);
        while (true) {
            TypeMirror superClassType = currentClassElement.getSuperclass();
            System.out.println(superClassType.toString());
            if (superClassType.getKind() == TypeKind.NONE) {
                System.out.println("结束        "+className);
                return false;
            }

            if (superClassType.toString().equals(className)) {
                System.out.println("结束        "+className);
                return true;
            }

            currentClassElement = (TypeElement) typeUtils.asElement(superClassType);
        }
    }

    public String getSimpleClassName(String className) {
        int dotIndex = className.lastIndexOf('.');
        if (dotIndex == -1) {
            return className;
        } else {
            return className.substring(dotIndex + 1);
        }
    }
}
